/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.platform.web.controller;


import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONArray;
import com.wealth.platform.entity.SldConfig;
import com.wealth.platform.enums.SldConfigTypeEnum;
import com.wealth.platform.service.ISldConfigService;
import com.wealth.platform.vo.SldConfigVO;
import com.wealth.system.entity.Corporation;
import com.wealth.system.entity.User;
import com.wealth.system.service.ShiroUser;
import com.wealth.system.web.controller.BaseController;

import iaas.redis.RedisService;
import iaas.utils.RedirectMessage;
import iaas.web.JqGridRequest;
import iaas.web.JqGridResponse;
import iaas.web.JqGridUtil;

/**
 * @author chan
 */
 @Controller
@RequestMapping(value="/manage/platform/sldConfig")
public class SldConfigController extends BaseController{

	Logger logger = Logger.getLogger(SldConfigController.class);
	
	@Autowired
	private RedisService redisService;
	@Autowired
	private ISldConfigService sldConfigService;
	
	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Classify对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
	 */
	@ModelAttribute
	public void init(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model) {
		try{
			if (id != -1) {
//				SldConfig sldConfig = sldConfigService.get(id);
//				model.addAttribute("sldConfig", sldConfig);
			}
		}catch(Exception e){
			logger.error("初始化错误", e);
		}
	}
	
	/**
	 * 列表
	 */
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(Model model){
		Long corpid = ShiroUser.getInstance().getUser().getCorp().getId();
		model.addAttribute("corpid", corpid);
		return "platform/sldConfig/list";
	}
	
	
	/**
	 * 列表ajax查询数据
	 */
	@RequestMapping(value="/list",method = RequestMethod.POST)
	@ResponseBody
	public JqGridResponse getJson(Model model,JqGridRequest request) {
		try{

			Long corpid = ShiroUser.getInstance().getUser().getCorp().getId();
			
			Page<SldConfig> page = sldConfigService.get(corpid, request,SldConfig.class);
			
			return JqGridUtil.getJqGrirdResponse(page);
		}catch(Exception e){
			logger.error("列表错误！", e);
			return null;
		}
	}
	
	
	/**
	 * 创建或修改页面
	 */
	@RequestMapping(value="/saveOrUpdate",method = RequestMethod.GET)
	public String createForm(Model model,@RequestParam Long corpid){
		try {
			SldConfig sldConfigWechat = sldConfigService.getSldConfigByCorpIdAndType(corpid,SldConfigTypeEnum.WECHAT.getValue());
			SldConfig sldConfigPc = sldConfigService.getSldConfigByCorpIdAndType(corpid,SldConfigTypeEnum.PC.getValue());
			model.addAttribute("sldConfigWechat", sldConfigWechat);
			model.addAttribute("sldConfigPc", sldConfigPc);
			
			model.addAttribute("corpid", corpid);
			return "platform/sldConfig/config";
		} catch (Exception e) {
			logger.error("创建页面错误", e);
			return "system/500";
		}
	}
	
	/**
	 * 创建或修改
	 */
	@RequestMapping(value="/saveOrUpdate",method = RequestMethod.POST)
	public String create(Model model,Long corpid,String pcDomain,String wechatDomain, RedirectAttributes redirectAttributes){
		try {
			User user = ShiroUser.getInstance().getUser();
			
			
			//新增或修改，MDZZ 这段代码写的好...
			SldConfig sldConfigWechat = sldConfigService.getSldConfigByCorpIdAndType(corpid,SldConfigTypeEnum.WECHAT.getValue());
			SldConfig sldConfigPc = sldConfigService.getSldConfigByCorpIdAndType(corpid,SldConfigTypeEnum.PC.getValue());
			
			sldConfigService.clear();
			if(sldConfigWechat==null){
				sldConfigWechat = new SldConfig();
				sldConfigWechat.setCorp(new Corporation(corpid));
				sldConfigWechat.setCreator(user.getUsername());
				sldConfigWechat.setCreateTime(new Date());
				sldConfigWechat.setDomain(wechatDomain);
				sldConfigWechat.setType(SldConfigTypeEnum.WECHAT.getValue());
			}else{
				sldConfigWechat.setDomain(wechatDomain);
			}
			sldConfigService.save(sldConfigWechat);
			
			if(sldConfigPc==null){
				sldConfigPc = new SldConfig();
				sldConfigPc.setCorp(new Corporation(corpid));
				sldConfigPc.setCreator(user.getUsername());
				sldConfigPc.setCreateTime(new Date());
				sldConfigPc.setDomain(pcDomain);
				sldConfigPc.setType(SldConfigTypeEnum.PC.getValue());
			}else{
				sldConfigPc.setDomain(pcDomain);
			}
			sldConfigService.save(sldConfigPc);
		
			
			//加载企业二级域名配置
			List<SldConfigVO> sldConfgList = sldConfigService.getSldConfigVoList();
			String config = JSONArray.toJSONString(sldConfgList);
			redisService.set("redis_sld_config", config);
			
			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipSuccess("创建成功"));
			return "redirect:/manage/platform/sldConfig/list";
		} catch (Exception e) {
			logger.error("创建错误", e);
			return "system/500";
		}
	}
}

